
#include "../../engine.h"

#include "../../User.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Bag.h"

#include "matching.h"

namespace ProjectZ { namespace NCommand { namespace NMatching {

void CS_DUNGEON_GET_ITEM::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * /// 던젼아이템 획드 C -> S
	CS_DUNGEON_GET_ITEM		0x0822
	{
		u16 u16item_seq;		/// 아이템 드롭 시퀀스(SC_DUNGEON_ITEM_DROP에서 부여)
	}

	/// 던젼아이템 획드 S -> C
	SC_DUNGEON_GET_ITEM		0x0823
	{
		s16	s16ack;				/// 인벤공간 없음 등 ack값 정의 필요
	}
	 */

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_DUNGEON_GET_ITEM NULL == pSpace GID : %d", pUser->GetUserSeq());
		return;
	}

	if (false == pSpace->IsDungeon())
	{
		DEBUGLOG("CS_DUNGEON_GET_ITEM TYPE IS NOT DUNGEON GID : %d", pUser->GetUserSeq());
		return;
	}

	PREPARE_PACKET;

	uint16_t drop_item_seq = 0;
	* pPacket >> drop_item_seq;

	NLogic::Item * pItem = pSpace->PopItem(drop_item_seq);
	if (NULL == pItem)
	{
//		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
//		* pAck << static_cast<uint16_t>(ACK_NO_DATA);
//		pAck->MakePacket(CMD_SC_DUNGEON_GET_ITEM);
//		pUser->Send(pAck);

		DEBUGLOG("CS_DUNGEON_GET_ITEM NULL == pItem ITEM SEQ : %d GID : %d", drop_item_seq, pUser->GetUserSeq());
		return;
	}

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(pItem->GetData().bag_type));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_DUNGEON_GET_ITEM NULL == pBag ITEM SEQ : %d GID : %d", drop_item_seq, pUser->GetUserSeq());
		return;
	}

//    // 인벤토리가 꽉 찬 경우
//    if (true == pBag->IsBagFull())
//    {
//        NLogic::Static::Instance().DEL_ITEM(pItem);
//        return;
//    }

    bool bBagFull = false;
	if (NULL == pBag->AddItemWithData(pItem->GetData(), bBagFull))
	{
		if (true == bBagFull)
		{
			DEBUGLOG("CS_DUNGEON_GET_ITEM true == bBagFull ITEM SEQ : %d", drop_item_seq);
		}
		else
		{
			DEBUGLOG("CS_DUNGEON_GET_ITEM NULL == pBag->AddItemWithData(pItem->GetData(), bBagFull) ITEM SEQ : %d", drop_item_seq);
		}
		NLogic::Static::Instance().DEL_ITEM(pItem);
		return;
	}
	NLogic::Static::Instance().DEL_ITEM(pItem);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	pAck->Write2(ACK_OK);
	pAck->MakePacket(CMD_SC_DUNGEON_GET_ITEM);

	pUser->Send(pAck);
}

} /*NBattle*/ } /*NCommand*/ } /*ProjectZ*/
